package lambdasinaction.chap4;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * TODO
 *
 * @author Fish
 * @since 0.0.1 2018/7/23 23:41
 */
public class MyStreamBasic {

  public static void main(String[] args) {
    // Java7
    getLowCaloricDishesNamesInJava7(MyDish.menu).forEach(System.out::println);

    System.out.println("------------");

    // Java8
    getLowCaloricDishesNamesInJava8(MyDish.menu).forEach(System.out::println);
  }

  public static List<String> getLowCaloricDishesNamesInJava7(List<MyDish> dishes) {
    List<MyDish> lowCaloricDishes = new ArrayList<>();
    for (MyDish dish : dishes) {
      if (dish.getCalories() < 400) {
        lowCaloricDishes.add(dish);
      }
    }
    List<String> lowCaloricDishesName = new ArrayList<>();
    Collections.sort(lowCaloricDishes, new Comparator<MyDish>() {
      @Override
      public int compare(MyDish o1, MyDish o2) {
        return Integer.compare(o1.getCalories(), o2.getCalories());
      }
    });
    for (MyDish dish : lowCaloricDishes) {
      lowCaloricDishesName.add(dish.getName());
    }
    return lowCaloricDishesName;
  }

  public static List<String> getLowCaloricDishesNamesInJava8(List<MyDish> dishes) {
    return dishes.stream().filter(d -> d.getCalories() < 400)
        .sorted(Comparator.comparing(MyDish::getCalories)).map(MyDish::getName).collect(Collectors
            .toList());

  }

}
